<template>
  <div>
    <div class="top-panel__box">
      <el-form :model="dataForm" ref="dataForm" inline size="small" onsubmit="return false">
        <el-form-item>
          <el-input placeholder="输入归属机构编号" v-model="dataForm.organizationCode" maxlength="30" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList(1)">
            <fa-icon class="fa-search" />查询</el-button>
          <reset-button :dataForm.sync="dataForm" :fixedData="{pageNum: 1, pageSize: 10}" />
        </el-form-item>
      </el-form>
    </div>
    <el-row class="button-box">
      <el-col>
        <el-button type="primary" size="small" @click="addInfor" v-permission="'FHNAME_NAME'">
          <fa-icon class="fa-plus" />新增分行</el-button>
        <tooltip :tooltipOption="tooltipOption" />
      </el-col>
    </el-row>
    <el-row class="table-top">
      <el-table :data="dataList" v-loading="tableLoading" tooltip-effect="light" class="tc-table" size="small">
        <el-table-column type="index" width="55" fixed="left" label="序号"></el-table-column>
        <template v-for="(column, index) in FOLLOW_HEAD_LIST">
          <el-table-column :prop="column.prop" :label="column.label" :key="index" :width="column.width" :show-overflow-tooltip="true" />
        </template>
        <el-table-column prop="operation" class-name="oper-col" label="操作" :width="300">
            <template slot-scope="scope">
              <el-button size="mini" type="primary" plain @click="showEditor(scope.row)" v-permission="'FHNAME_DTAILS'">详情</el-button>
              <el-button size="mini" type="primary" plain @click="editUserList(scope.row)" v-permission="'FHNAME_GLCY'">管理成员</el-button>
              <el-button size="mini" type="danger" plain @click="deletPersonInfo(scope.row)" v-permission="'FHNAME_DELETE'">删除</el-button>
            </template>
          </el-table-column>
      </el-table>
      <el-row class="pagination-top">
        <pagination :pageNum.sync="dataForm.pageNum" :curSizes.sync="dataForm.pageSize" :total="total" @getList="getList" />
      </el-row>
    </el-row>
    <el-dialog :title="editData.organizationId ? '详情' : '新增分行'" :visible.sync="dialog.addBranchDialog" width="40%" :close-on-click-modal="false">
      <add-branch
      v-if="dialog.addBranchDialog"
      :editData="editData"
      @showAddUser="showUserEvent"
      :listValue="addForm.managerList"
      @updataSuccess="updateInfo"
      @close="dialog.addBranchDialog = false"/>
    </el-dialog>
    <el-dialog title="下属客户经理" :visible.sync="dialog.addUserDialog" width="30%" :close-on-click-modal="false">
      <add-user
      v-if="dialog.addUserDialog"
      @update="updateUser"
      @close="dialog.addUserDialog = false"
      :managerList="addForm.managerList"/>
    </el-dialog>
  </div>
</template>
<script>
import api from '../api'
import { CONSTANT_OBJ } from '@/utils/constant'
import { FOLLOW_HEAD_LIST } from '../js/contant.js'
import { common } from '@/utils/common'
import AddBranch from './AddBranch'
import AddUser from './AddUser'
export default {
  components: {AddBranch, AddUser},
  data () {
    return {
      FOLLOW_HEAD_LIST,
      tableLoading: false,
      dataForm: {
        organizationCode: '',
        organizationName: '',
        pageNum: 1,
        pageSize: 10
      },
      dataList: [],
      editorDialog: false,
      dialog: {
        addBranchDialog: false,
        addUserDialog: false
      },
      total: 0,
      productNoList: [],
      editData: {},
      uploadDialog: false,
      isEdit: '',
      tooltipOption: {
        title: '帮助提示',
        options: [
          '1、DS团队进件，需将业绩挂靠至分行及分行客户经理名下。本功能实现业绩归属关系配置。',
          '2、配置业绩归属关系，需（1）配置分行及分行客户经理名单（2）配置DS团队业绩挂靠的分行。',
          '3、若一个DS团队挂靠多个分行，上送业绩时随机上送至其中一个分行处。',
          '4、若一个分行配置多个客户经理，上送时业绩随机上送至其中一位客户经理处。',
          '5、当前有DS团队挂靠的分行无法被删除，需删除所有挂靠关系后才可删除分行。'
        ]
      },
      addForm: {
        managerList: [],
        organizationCode: '',
        organizationName: ''
      }
    }
  },
  mounted () {
    this.getList()
  },
  methods: {
    // 查询列表
    getList (val) {
      this.dataForm.pageNum = val || this.dataForm.pageNum
      this.tableLoading = true
      api.queryPreformanceList(this.dataForm).then(res => {
        const { responseCode, data } = res
        if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
          this.dataList = data.list || []
          this.total = parseInt(data.total) || 0
        }
      }).finally(() => {
        this.tableLoading = false
      })
    },
    addInfor () {
      this.editData = {}
      this.addForm.managerList = []
      this.dialog.addBranchDialog = true
      this.isEdit = 'add'
    },
    showEditor (row) {
      this.isEdit = 'detail'
      api.queryPreformanceDetails(row.organizationId).then(res => {
        this.editData = {...res.data}
        this.addForm.managerList = [...res.data.managerList]
        this.dialog.addBranchDialog = true
      })
    },
    editUserList (row) {
      this.isEdit = 'user'
      api.queryPreformanceDetails(row.organizationId).then(res => {
        this.editData = {...res.data}
        this.addForm.managerList = [...res.data.managerList]
        this.addForm.organizationCode = row.organizationCode
        this.addForm.organizationName = row.organizationName
        this.dialog.addUserDialog = true
      })
    },
    close (data) {
      console.log(data)
      this.editorDialog = data
    },
    deletPersonInfo (row) {
      common.confirmFn(`请确认是否移除归属机构${row.organizationName}？`, '移除归属机构').then(res => {
        if (res) {
          api.deletePreformance(row.organizationId).then(res => {
            this.successFn(res)
          })
        }
      })
    },
    showUserEvent () {
      this.dialog.addUserDialog = true
    },
    updateUser (users) {
      console.log(users, '客经')
      this.addForm.managerList = [...users]
      this.dialog.addUserDialog = false
      if (this.isEdit === 'user') {
        this.updateInformation()
      }
    },
    updateInfo (data) {
      this.addForm.organizationCode = data.organizationCode
      this.addForm.organizationName = data.organizationName
      if (this.isEdit === 'detail') {
        this.updateInformation()
        this.dialog.addBranchDialog = false
      } else if (this.isEdit === 'add') {
        api.addPreformance(this.addForm).then(res => {
          this.successFn(res)
          this.dialog.addBranchDialog = false
        })
      }
    },
    updateInformation () {
      this.addForm.organizationId = this.editData.organizationId
      api.updatePreformance(this.addForm).then(res => {
        this.successFn(res)
      })
    },
    successFn (res, dialog) {
      const { responseCode, responseMsg: message } = res
      if (responseCode === CONSTANT_OBJ.RES_SUCCESS) {
        this.$message({
          message,
          type: 'success'
        })
        if (dialog) {
          this[dialog] = false
        }
        this.getList(1)
      }
    }
  }
}
</script>
<style lang="less" scoped>
.button-box{
  margin-bottom: 30px;
}
</style>
